package com.emmmya.ocs.modules.system.controller;

import com.emmmya.ocs.common.constant.RedisConstant;
import com.emmmya.ocs.common.utils.JwtUtils;
import com.emmmya.ocs.config.redis.RedisManager;
import com.emmmya.ocs.modules.system.file.entity.Directory;
import com.emmmya.ocs.modules.system.file.entity.File;
import com.emmmya.ocs.modules.system.file.service.DirectoryService;
import com.emmmya.ocs.modules.system.file.service.FileService;
import com.emmmya.ocs.modules.system.file.utils.DirAndFileUtils;
import com.emmmya.ocs.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller()
public class UIController {
    @Autowired
    RedisManager redisManager;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    UserService userService;

    /**
     * 文件夹管理器
     */
    @Autowired
    DirectoryService directoryService;

    /**
     * 文件管理器
     */
    @Autowired
    private FileService fileService;

    @Value("${config.jwt.header}")
    private String header;

    //按键插件
    @RequestMapping(value = {"/button/index"})
    public String button(){
        return "button/index";
    }

    //百度富文本编辑器
    @RequestMapping(value = {"/demo"})
    public String demo(){
        return "demo";
    }


    @RequestMapping(value = {"/","/index"})
    public String index(){
        //return "index";
        return "redirect:admin/index2";
    }
    //登陆页面
    @RequestMapping(value = {"login"})
    public String login(Model model,HttpServletRequest request){
        //随机生成一个uuid，用作于登陆的标识
        String uuid = UUID.randomUUID().toString().replace("-","");
        Object targetURI = request.getAttribute("targetURI");
        if(targetURI == null){
            targetURI = request.getParameter("targetURI");
        }
        //该标识存储在redis中
        redisManager.set(RedisConstant.LOGIN_TOKEN+uuid,"true",300);
        model.addAttribute("loginToken",uuid);
        model.addAttribute("jumpUrl",targetURI);
        return "login";
    }

//    @ApiOperation(value = "跳转文件操作页面")
    @RequestMapping(value = "toFileUI")
    public String toFileUI(){
        return "/fileManagement";
    }

    @RequestMapping(value = {"register"})
    public String register(){
        return "register";
    }


    @RequestMapping(value={"fileManagement"})
    public String fileManagement(@RequestParam(value = "isIndex",required = false) String isIndex, Model model){
        //进入默认根目录
        Directory root = directoryService.getRoot();
        if(root==null){
            model.addAttribute("errorMsg","未查询到根目录");
        }else{
            model.addAttribute("root",root);
            List<String> paths = DirAndFileUtils.parsePath(root.getPath());
            List<Directory> cliDir = directoryService.findByParentId(root.getId());
            List<File> cliFiles = fileService.findByDirId(root.getId());
            model.addAttribute("paths",paths);
            model.addAttribute("cliDir",cliDir);
            model.addAttribute("cliFiles",cliFiles);
            model.addAttribute("isIndex",isIndex);
        }
        return "file/fileOperation";
    }

    @RequestMapping(value ={"jump"})
    public String jump(HttpServletRequest request,Model model) throws IOException {
        // 登录按钮使用ajax方式时候
        // String token = request.getParameter(this.header);
        String token = request.getHeader(this.header);
        String url  = request.getRequestURI();
        System.out.println("jump的url："+url);
        if(token != null) {
            String rToken = (String) redisManager.get(RedisConstant.REDIS_USER_TOKEN + jwtUtils.getUsername(token));
            if(token.equals(rToken)){
                model.addAttribute("token",token);
                //保存一开始请求的url
                model.addAttribute("url","/index");
            }
        }else{
            return "redirect:/login";
        }
        return "redirect:/index";
    }

}
